package com.woniux.entity.factoryUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author 佘赛飞
 * @create 2021 -11 -09 5:48 PM
 */
public class BeanFactory {

    private static Properties properties;
    private static Map map;

    static {
       String path="applicationContext.properties";
       //创建properties对象
       properties = new Properties();
       //读取文件，转成输入流
        InputStream inputStream = BeanFactory.class.getResourceAsStream(path);
        //实例话map
        map = new HashMap();
        try {
            //读取输入流
            properties.load(inputStream);
            //从这个流文件中，拿到对应的想要的东西
            Enumeration<Object> keys = properties.keys();
            //key是配置文件中所有的等号的值，进行while遍历循环
            while (keys.hasMoreElements()){
                String key = keys.nextElement().toString();
                //获取等号右边的值
                String value = properties.getProperty(key);
                //通过这个字符串，再通过反射，获取这个类路径的对象
                Object valueObject = Class.forName(value).newInstance();
                map.put(key,valueObject);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    //获取实例化对象！new 类！传入一个别名，获取对应别名的对象
    public static Object getBean(String alias){
      return map.get(alias);
    }
}
